package com.dongdongshop.controller;

import com.dongdongshop.common.AjaxResult;
import com.dongdongshop.common.ResultEnum;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.TbUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private RedisTemplate redisTemplate;
    Logger logger = LoggerFactory.getLogger(UserController.class);

    @DubboReference
    private TbUserService tbUserService;

    /*用户登录页面跳转*/
    @RequestMapping("userLogin")
    public String userLogin(){
        return "login";
    }
    /*用户注册页面跳转*/
    @RequestMapping("userRegister")
    public String userRegister(){
        return "register";
    }

    /*注册账号*/
    @RequestMapping("generateVerification")
    @ResponseBody
    public AjaxResult generateVerification(String sms,TbUser user){
        String str = (String)redisTemplate.boundValueOps(user.getPhone()).get();
        if (user == null){
            return AjaxResult.R(ResultEnum.ERROR);
        }else if (!str.equals(sms)){
            return AjaxResult.R(ResultEnum.SMS_NO);
        }
        try {
            int i = tbUserService.insertTbUser(user);
            if (i>0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("客户注册失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }

    }
    /*登录*/
    @RequestMapping("getLogin")
    @ResponseBody
    public AjaxResult getLogin(TbUser user){
        Logger logger = LoggerFactory.getLogger(UserController.class);
        /*通过shrio认证登录*/
        Subject subject = SecurityUtils.getSubject();
        /*封装用户数据*/
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());
        try {
            subject.login(token);
            /*登陆成功跳转首页*/
            return AjaxResult.R(ResultEnum.SUCCESS);
        }catch (UnknownAccountException e){
            /*用户名不存在*/
            System.out.println("用户名不存在");
            logger.error("登录:用户名不存在!错误内容:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (IncorrectCredentialsException e){
            System.out.println("密码错误");
            logger.error("登录:密码错误!错误内容:{}",e);
            return AjaxResult.ERROR();
        }
    }
    /*获取登录账号*/
    public String getUserName(){
        TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
        return user.getUsername();
    }
}
